<?php

/**
 * @author: biggege
 * @since:  2021-09-02 14:46:03
 */
namespace App\Validations\Manage;

use App\Helpers\ManageValidator;
use App\Models\ReturnVisit;
use Validator;

trait ReturnVisitValidator
{
    use ManageValidator;

    protected $validatorAttributes = [
        'town_super_id' => '乡镇督导员ID',
        'care_counselor_id' => '婴幼儿养育指导师ID',
        'children_id' => '家访儿童',
        'subject_id' => '家访编码',
        'home_visit_time' => '家访日期',
        'home_visit_status' => '是否家访',
        'home_visit_status_non_reason' => '未家访原因',
        'home_visit_duration' => '家访随访时间',
        'home_visit_children_status' => '本周家访儿童掌握状态',
        'return_visit_name' => '回访接话人姓名',
        'return_visit_guardian' => '回访接话人与儿童关系',
        'return_visit_time' => '电话回访时间',
        'return_visit_home_visit_status' => '看护人对家访工作是否满意',
        'return_visit_content' => '回访内容摘要',
    ];

    // 更新
    private function updateValidation(array $data) {
        if($data['home_visit_status'] == ReturnVisit::HOME_VISIT_WHETHER_NON) {
            unset($data['home_visit_duration']);
            unset($data['home_visit_children_status']);
            unset($data['return_visit_name']);
            unset($data['return_visit_guardian']);
            unset($data['return_visit_time']);
            unset($data['return_visit_home_visit_status']);
            unset($data['return_visit_content']);
        }
        if($data['home_visit_status'] == ReturnVisit::HOME_VISIT_WHETHER_YES) {
            unset($data['home_visit_status_non_reason']);
        }
        $validator = Validator::make($data, [
            'town_super_id' => 'required|integer',
            'care_counselor_id' => 'required|integer',
            'children_id' => 'required|integer',
            'subject_id' => 'required|integer',
            'home_visit_time' => 'required|date_format:"Y-m-d"',
            'home_visit_status' => 'required|integer|min:1|max:2',
            'home_visit_status_non_reason' => 'required_if:home_visit_status,'.ReturnVisit::HOME_VISIT_WHETHER_NON.'|string|min:1|max:30',
            'home_visit_duration' => 'required_if:home_visit_status,'.ReturnVisit::HOME_VISIT_WHETHER_YES.'|integer',
            'home_visit_children_status' => 'required_if:home_visit_status,'.ReturnVisit::HOME_VISIT_WHETHER_YES.'|integer|min:1|max:3',
            'return_visit_name' => 'required_if:home_visit_status,'.ReturnVisit::HOME_VISIT_WHETHER_YES.'|string|min:1|max:30',
            'return_visit_guardian' => 'required_if:home_visit_status,'.ReturnVisit::HOME_VISIT_WHETHER_YES.'|integer|min:1|max:9',
            'return_visit_time' => 'required_if:home_visit_status,'.ReturnVisit::HOME_VISIT_WHETHER_YES.'|date_format:"Y-m-d H:i"',
            'return_visit_home_visit_status' => 'required_if:home_visit_status,'.ReturnVisit::HOME_VISIT_WHETHER_YES.'|integer|min:1|max:5',
            'return_visit_content' => 'required_if:home_visit_status,'.ReturnVisit::HOME_VISIT_WHETHER_YES.'|string|min:1|max:500',
        ], $this->validatorMessage, $this->validatorAttributes);

        if ($validator->fails()) {
            $errors = $validator->errors()->first(); // all()
            return $this->error(400, $errors); // implode(' | ', $errors)
        }
    }


}